package com.dream.keigezhushou.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.GlobalConst.GlobalConst;
import com.dream.keigezhushou.Activity.Uiutils.PrefUtils;
import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GuideActivity extends AppCompatActivity {


    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_des)
    TextView mTvDes;
    @BindView(R.id.llDotContainer)
    LinearLayout mLlDotContainer;

    @BindView(R.id.btn_login)
    Button mBtnLogin;
    @BindView(R.id.btn_experience)
    Button mBtnExperience;
    @BindView(R.id.rl_showLogin)
    RelativeLayout mRlShowLogin;

    private int[] mResIds = new int[]{R.mipmap.vp1,R.mipmap.vp2,
            R.mipmap.vp3, R.mipmap.vp4};
    private  String [] mTitle = new String[]{"挑好歌","大声唱","约歌去","城会玩"};
    private  String [] mDes = new String[]{"好歌速选，30秒就爱上","好歌速选，30秒就爱上","朋友小聚，预定K歌","城里玩法，新鲜奇特"};
    private   int                     distance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        initview();
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳登陆/注册页面

            }
        });
        mBtnExperience.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳主页面
                startActivity(new Intent(GuideActivity.this,MainActivity.class));
                //缓存
                PrefUtils.putBoolean(getApplicationContext(), GlobalConst.PREFUL_ISFIRSTIN,false);
                finish();
            }
        });

    }

    private void initview() {

        mViewPager.setAdapter(new ViewPagerAdapter());

        for (int i = 0; i < mResIds.length; i++) {
            ImageView ivDot = new ImageView(this);
            if (i > 0) {

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                int px = UiUtils.dip2px(this, 10);
                params.leftMargin = px;
                ivDot.setLayoutParams(params);
            }
            ivDot.setImageResource(R.drawable.shape_dot_grey);
            mLlDotContainer.addView(ivDot);
        }
        final ImageView ivRed = (ImageView) findViewById(R.id.ivRed);

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if (position == mResIds.length - 1) {
                    mRlShowLogin.setVisibility(View.VISIBLE);
                } else {
                    mRlShowLogin.setVisibility(View.GONE);
                }

            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

                RelativeLayout.LayoutParams layoutParams = (android.widget.RelativeLayout.LayoutParams) ivRed
                        .getLayoutParams();
                layoutParams.leftMargin = (int) (distance * (positionOffset + position));

                ivRed.setLayoutParams(layoutParams);
                mTvTitle.setText(mTitle[position]);
                mTvDes.setText(mDes[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mLlDotContainer.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {
                        distance =  mLlDotContainer.getChildAt(1).getLeft() -  mLlDotContainer.getChildAt(0).getLeft();

                        System.out.println("distance= " + distance);
                        mLlDotContainer.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(this);
                        // llDotContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                });
    }

    class  ViewPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return mResIds.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView iv = new ImageView(GuideActivity.this);
            iv.setImageResource(mResIds[position]);
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            container.addView(iv);
            return iv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

    }
}
